package http_servicer

import (
	//	"encoding/json"
	"fmt"
	"ifms/db_servicer"
	"net/http"
	"strconv"
)

// This script implement the function related with pond information,
//  majority about data in table pond_info
//
//    router.HandleFunc("/newpond",        newpondHandler).Methods("POST")
//    router.HandleFunc("/fetchpond",      fetchpondHandler).Methods("POST")
//    router.HandleFunc("/modifypond",     modifypondHandler).Methods("POST")

func newpondHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>>> New Pond Info :", log.LstdFlags|log.Lshortfile)

	session_id, user_id := getAccountName(request)

	//	logger.Println(">>>session_id:" , session_id,", user_id :", user_id  )

	if session_id == "" || user_id == 0 {
		reloginPage(response, request)
		return
	}

	pond_name := request.FormValue("pond_name")
	prod := request.FormValue("prod")
	source := request.FormValue("source")

	var area float64
	var height float64
	var density float64
	var earth_thickness float64

	v_area := request.FormValue("area")
	v_height := request.FormValue("height")
	v_density := request.FormValue("density")
	v_earth_thickness := request.FormValue("earth_thickness")

	var msg string = "Not an valid nubmer:"

	var err error

	if area, err = strconv.ParseFloat(v_area, 64); err != nil && v_area != "" {
		msg = msg + "  area           :" + v_area
	}
	if height, err = strconv.ParseFloat(v_height, 64); err != nil && v_height != "" {
		msg = msg + "  height         :" + v_height
	}
	if density, err = strconv.ParseFloat(v_density, 64); err != nil && v_density != "" {
		msg = msg + "  density        :" + v_density
	}
	if earth_thickness, err = strconv.ParseFloat(v_earth_thickness, 64); err != nil && v_earth_thickness != "" {
		msg = msg + "  earth_thickness:" + v_earth_thickness
	}

	if msg > "Not an valid nubmer:" {
		logger.Println("\nError:", err)
		logger.Println("\nMsg:", msg)
		fmt.Fprintf(response, `{"flag":-1,"message":"%s" }`, msg)
	} else {

		// logger.Println(
		//  " Pond_name      :", pond_name   ,
		//  ",prod           :", prod        ,
		//  ",source         :", source      ,
		//  ",area           :", area        ,
		//  ",height         :", height      ,
		//  ",density        :", density     ,
		//  ",earth_thickness:", earth_thickness	)

		dbmsg := db_servicer.New_Pond(user_id, pond_name, area, height, prod, density, source, earth_thickness)

		logger.Println("\n Dbmsg:", dbmsg)

		fmt.Fprint(response, dbmsg.Data)

	}

}

func fetchpondHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Ponds List:", log.LstdFlags|log.Lshortfile)

	session_id, user_id := getAccountName(request)

	logger.Println(">>>session_id:", session_id, ", user_id :", user_id)

	if user_id == 0 {
		reloginPage(response, request)
		return
	}

	//	var responseStr []byte

	dbmsg := db_servicer.Pond_List(user_id)

	logger.Println("\n Dbmsg:", dbmsg)
	fmt.Fprint(response, dbmsg.Data)

}

func modifypondHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Modify Ponds:", log.LstdFlags|log.Lshortfile)

	v_session_id, v_user_id := getAccountName(request)

	logger.Println(">>>session_id:", v_session_id, ", user_id :", v_user_id)

	if v_user_id == 0 {
		reloginPage(response, request)
		return
	}

	pond_name := request.FormValue("pond_name")
	prod := request.FormValue("prod")
	source := request.FormValue("source")

	v_density := request.FormValue("density")
	v_earth_thickness := request.FormValue("earth_thickness")
	v_area := request.FormValue("area")
	v_height := request.FormValue("height")

	v_pond_id := request.FormValue("pond_id")

	var pond_id int
	var density, earth_thickness, area, height float64

	var msg string = "Not an valid nubmer:"
	var err error

	if pond_id, err = strconv.Atoi(v_pond_id); err != nil && v_pond_id != "" {
		msg = msg + " pond_id          :" + v_pond_id
	}
	if density, err = strconv.ParseFloat(v_density, 64); err != nil && v_density != "" {
		msg = msg + " density          :" + v_density
	}
	if earth_thickness, err = strconv.ParseFloat(v_earth_thickness, 64); err != nil && v_earth_thickness != "" {
		msg = msg + " earth_thickness  :" + v_earth_thickness
	}
	if area, err = strconv.ParseFloat(v_area, 64); err != nil && v_area != "" {
		msg = msg + " area             :" + v_area
	}
	if height, err = strconv.ParseFloat(v_height, 64); err != nil && v_height != "" {
		msg = msg + " height           :" + v_height
	}

	if msg > "Not an valid nubmer:" {
		logger.Println("\nError:", err)
		logger.Println("\nMsg:", msg)
		fmt.Fprintf(response, `{"flag":-1,"message":"%s" }`, msg)
	} else {

		// logger.Println(  "pond_id:",            pond_id   ,
		// 				 ",pond_name:",         pond_name ,
		// 				 ",area:",              area      ,
		// 				 ",height:",            height    ,
		// 				 ",prod:",              prod      ,
		// 				 ",density:",           density   ,
		// 				 ",source:",            source    ,
		// 				 ",earth_thickness:",	earth_thickness	)

		dbmsg := db_servicer.Modify_Pond(v_user_id, pond_id, pond_name, prod, source, area, height, density, earth_thickness)

		logger.Println("\n Dbmsg:", dbmsg)
		fmt.Fprint(response, dbmsg.Data)

	}

}

func revokepondHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Revoke Pond from User:", log.LstdFlags|log.Lshortfile)

	v_session_id, v_user_id := getAccountName(request)

	logger.Println(">>>session_id:", v_session_id, ", user_id :", v_user_id)

	if v_user_id == 0 {
		reloginPage(response, request)
		return
	}

	v_pond_id := request.FormValue("pond_id")

	var pond_id int

	var msg string = "Not an valid nubmer:"
	var err error

	if pond_id, err = strconv.Atoi(v_pond_id); err != nil && v_pond_id != "" {
		msg = msg + " pond_id          :" + v_pond_id
	}

	if msg > "Not an valid nubmer:" {
		logger.Println("\nError:", err)
		logger.Println("\nMsg:", msg)
		fmt.Fprintf(response, `{"flag":-1,"message":"%s" }`, msg)
	} else {

		// logger.Println(  "pond_id:",            pond_id   	)

		dbmsg := db_servicer.Revoke_Pond(v_user_id, pond_id)

		logger.Println("\n Dbmsg:", dbmsg)
		fmt.Fprint(response, dbmsg.Data)

	}

}
